package service

import (
	"errors"
	"gin-bms/global"
	"gin-bms/model"
	"time"

	"gorm.io/gorm"
)

//拉黑Jwt
func JsonInBlacklist(jwtList model.JwtBlacklist) (err error) {
	err = global.GLOBAL_DB.Create(&jwtList).Error
	return
}

//判断jwt是否在黑名单内
func IsBlacklist(jwt string) bool {
	isNotFound := errors.Is(global.GLOBAL_DB.Where("jwt=?", jwt).First(&model.JwtBlacklist{}).Error, gorm.ErrRecordNotFound)
	return !isNotFound
}

//从redis取jwt
func GetRedisJWT(userName string) (err error, redisJWT string) {
	redisJWT, err = global.GLOBAL_REDIS.Get(userName).Result()
	return err, redisJWT
}

//jwt存入redis并设置过期时间
func SetRedisJWT(jwt string, userName string) (err error) {
	// 此处过期时间等于jwt过期时间
	timer := 60 * 60 * 24 * 7 * time.Second
	err = global.GLOBAL_REDIS.Set(userName, jwt, timer).Err()
	return err
}
